//
//  GXShopAuthenController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/26.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXShopAuthenController.h"
#import "GXIdentityExchangeView.h"
#import "GXIdentityfootPersonView.h"
#import "GXIdentityfootSellerView.h"
#import "GXIdentityCommitController.h"
#import "ImagePickerTool.h"
@interface GXShopAuthenController ()<IdentityExchangeViewDelegate,GXIdentityfootPersonViewDelegate,GXIdentityfootSellerViewDelegate>
@property (nonatomic,strong)UIScrollView * identityScrollView;
@property (nonatomic,strong)UITextField *nameTF;
@property (nonatomic,strong)UITextField *idcardTF;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIImageView *backImageView;
@property (nonatomic,strong)UIImageView *frontImageView;
@property (nonatomic,strong)UIView *frontView;
@property (nonatomic,strong)UIView *backView;
@property (nonatomic,strong) UIButton * frontBtn;
@property (nonatomic,strong) UIButton * backBtn;
@property (nonatomic,strong)GXIdentityfootPersonView * personView;
@property (nonatomic,strong)GXIdentityfootSellerView * sellerView;
@property (nonatomic,strong)GXIdentityExchangeView * exchangeView;
@property (nonatomic,strong) UIButton * commitBtn;
@property (nonatomic,assign)BOOL userType;
@property (nonatomic,copy)NSString *saveFrontImageStr;
@property (nonatomic,copy)NSString *saveBackImageStr;
@property (nonatomic,copy)NSString *savePersonImageStr;
@property (nonatomic,copy)NSString *saveSellerHeadImageStr;
@property (nonatomic,copy)NSString *saveSellerFootImageStr;
@end

@implementation GXShopAuthenController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kGlobalbgColor;
    self.title = @"认证";
    [self creatAllView];
}
- (void)creatAllView
{
    [self.view addSubview:self.identityScrollView];
    UIView * textFieldView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, kUIScreenWidth-20, 81)];
    textFieldView.layer.masksToBounds = YES;
    textFieldView.layer.cornerRadius = 3;
    textFieldView.backgroundColor = [UIColor whiteColor];
    [self.identityScrollView addSubview:textFieldView];
    UIView *lineView =[[UIView alloc]initWithFrame:CGRectMake(10, 40, kUIScreenWidth-40, 1)];
    lineView.backgroundColor = RGBColor(244, 244, 244);
    [textFieldView addSubview:lineView];
    [textFieldView addSubview:self.nameTF];
    [textFieldView addSubview:self.idcardTF];
    [self.identityScrollView addSubview:self.titleLabel];
    [self.identityScrollView addSubview:self.frontView];
    [self.frontView addSubview:self.frontImageView];
    [self.frontView addSubview:self.frontBtn];
    [self.identityScrollView addSubview:self.backView];
    [self.backView addSubview:self.backImageView ];
    [self.backView addSubview:self.backBtn];
    [self.identityScrollView addSubview:self.exchangeView];
    [self.identityScrollView addSubview:self.personView];
    [self.view addSubview:self.commitBtn];
}

- (UIScrollView *)identityScrollView
{
    if (_identityScrollView == nil) {
        _identityScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight-45-Height_NavBar)];
        _identityScrollView.showsVerticalScrollIndicator = NO;
        _identityScrollView.showsHorizontalScrollIndicator = NO;
        _identityScrollView.scrollEnabled = YES;
        _identityScrollView.userInteractionEnabled = YES;
    }
    return _identityScrollView;
}
- (UITextField *)nameTF
{
    if (_nameTF == nil) {
        _nameTF= [[UITextField alloc]initWithFrame:CGRectMake(8, 0, kUIScreenWidth-28, 40)];
        [self setTextFieldProperty:_nameTF placeHolderStr:@"请输入真实姓名"];
    }
    return _nameTF;
}
- (UITextField *)idcardTF
{
    if (_idcardTF == nil) {
        _idcardTF = [[UITextField alloc]initWithFrame:CGRectMake(8, 41, kUIScreenWidth-28, 40)];
        [self setTextFieldProperty:_idcardTF placeHolderStr:@"请输入身份证号"];
    }
    return _idcardTF;
}
- (void)setTextFieldProperty:(UITextField *)textField placeHolderStr:(NSString *)str
{
    textField.borderStyle =UITextBorderStyleNone;
    textField.placeholder =str;
    textField.textAlignment = NSTextAlignmentLeft;
    textField.font = [UIFont systemFontOfSize:12];
    textField.backgroundColor = [UIColor clearColor];
}
- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 150, 15)];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.text = @"证件照正反面(0/2)";
        _titleLabel.textColor = [UIColor darkGrayColor];
    }
    return _titleLabel;
}
- (UIView *)frontView
{
    if (_frontView == nil) {
        _frontView =[[UIView alloc]initWithFrame:CGRectMake(10, 130, kUIScreenWidth-20, 150)];
        _frontView.backgroundColor = [UIColor whiteColor];
    }
    return _frontView;
}
- (UIImageView *)frontImageView
{
    if (_frontImageView == nil) {
        _frontImageView = [[UIImageView alloc]init];
        _frontImageView.frame = self.frontView.bounds;
    }
    return _frontImageView;
}
- (UIButton *)frontBtn
{
    if (_frontBtn == nil) {
        _frontBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_frontBtn setImage:[UIImage imageNamed:@"home_IDCardup"] forState:UIControlStateNormal];
        [_frontBtn addTarget:self action:@selector(pressFrontBtn:) forControlEvents:UIControlEventTouchUpInside];
        _frontBtn.frame = CGRectMake((kUIScreenWidth-145)/2, 37.5, 125, 75);
    }
    return _frontBtn;
}
- (UIView *)backView
{
    if (_backView == nil) {
        _backView =[[UIView alloc]initWithFrame:CGRectMake(10, 290, kUIScreenWidth-20, 150)];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}
- (UIImageView *)backImageView
{
    if (_backImageView == nil) {
        _backImageView = [[UIImageView alloc]init];
        _backImageView.frame = self.backView.bounds;
    }
    return _backImageView;
}
- (UIButton *)backBtn
{
    if (_backBtn ==nil) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"home_IDCarddown"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(pressBackBtn:) forControlEvents:UIControlEventTouchUpInside];
        _backBtn.frame = CGRectMake((kUIScreenWidth-145)/2, 37.5, 125, 75);
    }
    return _backBtn;
}

- (GXIdentityExchangeView *)exchangeView
{
    if (_exchangeView == nil) {
        _exchangeView = [GXIdentityExchangeView exchangeView];
        _exchangeView.delegate = self;
        _exchangeView.frame = CGRectMake(0, 460, kUIScreenWidth, 66);
    }
    return _exchangeView;
}
- (GXIdentityfootPersonView *)personView
{
    if (_personView == nil) {
        _personView  = [GXIdentityfootPersonView footPersonView];
        _personView.frame = CGRectMake(0, 526, kUIScreenWidth,210);
        _personView.delegate = self;
        self.userType =YES;
    }
    return _personView;
}

- (GXIdentityfootSellerView *)sellerView
{
    if (_sellerView == nil) {
        _sellerView = [GXIdentityfootSellerView footSellerView];
        _sellerView.frame = CGRectMake(0, 526, kUIScreenWidth, 375);
        _sellerView.delegate = self;
        self.userType = NO;
    }
    return _sellerView;
}
#pragma 通过个人和商户确定ScrollView滑动高度
- (void)setUserType:(BOOL)userType
{
    _userType = userType;
    if (userType) {
        self.identityScrollView.contentSize = CGSizeMake(kUIScreenWidth, 740);
    }else
    {
        self.identityScrollView.contentSize = CGSizeMake(kUIScreenWidth, 905);
    }
}
- (UIButton *)commitBtn
{
    if (_commitBtn == nil) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_commitBtn setTitle:@"提交审核" forState:UIControlStateNormal];
        [_commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_commitBtn setBackgroundColor:RGBColor(91, 210, 195) forState:UIControlStateNormal];
        [_commitBtn addTarget:self action:@selector(pressCommitBtn:) forControlEvents:UIControlEventTouchUpInside];
        _commitBtn.frame = CGRectMake(0, kUIScreenHeight-45-Height_NavBar, kUIScreenWidth, 45);
    }
    return _commitBtn;
}

#pragma useImagePickerTool
- (void)userImagePickerToolWithFileName:(NSString *)fileName ImageView:(UIImageView *)imageView
{
    WeakSelf(weakSelf);
    [[ImagePickerTool sharePicker] showImagePicker:self allowEdit:YES complete:^(id param) {
        [weakSelf uploadPhotoToServerFrom:param fileName:fileName  ImageView:imageView];
    }];
}
#pragma uploadPhoto
- (void)uploadPhotoToServerFrom:(UIImage *)image fileName:(NSString *)fileName  ImageView:(UIImageView *)imageView
{
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
    [SVProgressHUD showWithStatus:@"上传中" maskType:SVProgressHUDMaskTypeClear];
    [HttpHelper uploadFileFormPostWithImageData:imageData fileName:fileName success:^(id responseObject) {
        if ([responseObject[@"cod"] isEqualToString:@"001"]) {
            [SVProgressHUD showSuccessWithStatus:@"上传成功"];
            imageView.image = image;
            [self handleUploadPhotoSucessdata:responseObject[@"describa"] fileName:fileName];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"describa"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"上传失败"];
    }];
}
- (void)handleUploadPhotoSucessdata:(NSString *)saveStr fileName:(NSString *)fileName
{
    if ([fileName isEqualToString:@"idcodeF"]) {
        self.saveFrontImageStr = saveStr;
        [self setTitleLabelText];
    }
    if ([fileName isEqualToString:@"idcodeB"]) {
        self.saveBackImageStr = saveStr;
        [self setTitleLabelText];
    }
    if ([fileName isEqualToString:@"person"]) {
        self.savePersonImageStr = saveStr;
    }
    if ([fileName isEqualToString:@"merchant1"]) {
        self.saveSellerHeadImageStr = saveStr;
    }
    if ([fileName isEqualToString:@"merchant2"]) {
        self.saveSellerFootImageStr = saveStr;
    }
}

#pragma 上传身份证正面
- (void)pressFrontBtn:(UIButton *)btn
{
    [self userImagePickerToolWithFileName:IdcodeF ImageView:self.frontImageView];
}
#pragma 上传身份证反面
- (void)pressBackBtn:(UIButton *)btn
{
    [self userImagePickerToolWithFileName:IdcodeB  ImageView:self.backImageView];
}
- (void)setTitleLabelText
{
    if ((![NSString isBlankString:self.saveFrontImageStr])||(![NSString isBlankString:self.saveBackImageStr])) {
        self.titleLabel.text = @"证件照正反面(1/2)";
    }
    if ((![NSString isBlankString:self.saveFrontImageStr])&&(![NSString isBlankString:self.saveBackImageStr])) {
        self.titleLabel.text = @"证件照正反面(2/2)";
    }
}
#pragma GXIdentityfootPersonViewDelegate
- (void)clickPersonViewUploadPhoteButton:(UIButton *)btn
{
    [self userImagePickerToolWithFileName:Person ImageView:self.personView.idcardImageView];
}
#pragma GXIdentityfootSellerViewDelegate
- (void)clickSellerViewUploadPhoteButton:(UIButton *)btn
{
    if (btn.tag == 81) {
        [self userImagePickerToolWithFileName:Merchant1  ImageView:self.sellerView.sellerHeadView];
    }else{
        [self userImagePickerToolWithFileName:Merchant2  ImageView:self.sellerView.sellerFootView];
    }
}
#pragma IdentityViewDelegate
- (void)clickExchangeButton:(UIButton *)btn
{
    if ([btn.titleLabel.text isEqualToString:@"个人"]) {
        [self.sellerView removeFromSuperview];
        self.userType = YES;
        [self.identityScrollView addSubview:self.personView];
    }else if([btn.titleLabel.text isEqualToString:@"商家"])
    {
        [self.personView removeFromSuperview];
        self.userType = NO;
        [self.identityScrollView addSubview:self.sellerView];
    }
}

#pragma commitButton
- (void)pressCommitBtn:(UIButton *)btn
{
    if (![self judgeShopAuthenRight]) {
        return;
    }
    NSString *userId = [AccountTool sharedAccountTool].account.users[@"userId"];
    if (self.userType) {
        [ShopAuthenticationManager userAuthenWithUserId:userId name:self.nameTF.text identityNum:self.idcardTF.text image:self.saveFrontImageStr fimage:self.saveBackImageStr userSige:@"0" identityImage:self.savePersonImageStr license:@"" flicense:@"" success:^(id responseObject) {
            if (responseObject) {
                [self pushIdentityCommitController];
            }
        } fail:^(id responseObject) {
        }];
    }else
    {
        [ShopAuthenticationManager userAuthenWithUserId:userId name:self.nameTF.text identityNum:self.idcardTF.text image:self.saveFrontImageStr fimage:self.saveBackImageStr userSige:@"1" identityImage:@"" license:self.saveSellerHeadImageStr flicense:self.saveSellerFootImageStr success:^(id responseObject) {
            if (responseObject) {
                [self pushIdentityCommitController];
            }
        } fail:^(id responseObject) {
            
        }];
    }
   
}
- (void)pushIdentityCommitController
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        GXIdentityCommitController *commitVC = [[GXIdentityCommitController alloc]init];
        [self.navigationController pushViewController:commitVC animated:YES];
    });
}
- (BOOL)judgeShopAuthenRight
{
    if ([NSString isBlankString:self.nameTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入姓名"];
        return NO;
    }
    if ([NSString isBlankString:self.idcardTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入身份证号"];
        return NO;
    }
    if (![NSString judgeIdentityCardValueUseString:self.idcardTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入有效地身份证号"];
        return NO;
    }
    if ([NSString isBlankString:self.saveFrontImageStr]) {
        [SVProgressHUD showErrorWithStatus:@"请上传正面照"];
        return NO;
    }
    if ([NSString isBlankString:self.saveBackImageStr]) {
        [SVProgressHUD showErrorWithStatus:@"请上传反面照"];
        return NO;
    }
    if (self.userType) {
        if ([NSString isBlankString:self.savePersonImageStr]) {
            [SVProgressHUD showErrorWithStatus:@"请上传手持身份证正面照"];
            return NO;
        }
    }else
    {
        if ([NSString isBlankString:self.saveSellerHeadImageStr]) {
            [SVProgressHUD showErrorWithStatus:@"请上传营业资格证书"];
            return NO;
        }
        if ([NSString isBlankString:self.saveSellerFootImageStr]) {
            [SVProgressHUD showErrorWithStatus:@"请上传食品安全证书"];
            return NO;
        }
    }
    return YES;
}


@end
